
/****** Object:  StoredProcedure [dbo].[ws_ins_mokejimas]    Script Date: 10/19/2013 09:22:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

--IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'ws_ins_mokejimas')
DROP PROCEDURE ws_ins_mokejimas
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ws_ins_mokejimas] (
		@paved_nr varchar(35),
		@op_data varchar(20),
		@g_saskaita varchar(25),
		@m_kodas_ks varchar(25),
		@m_pav varchar(40),
		@suma varchar(25),
		@val char(3),
		@riv_id varchar(10),
		@pid varchar(35)
	)
AS
BEGIN
	declare @id varchar(12)
	declare @data datetime
	declare @sask varchar(12)
	
	SET @data = convert(datetime,@op_data ,126)
	SELECT @id = REPLACE(STR( CAST(max([I04_KODAS_CH]) as bigint)+1, 12), SPACE(1), '0')  FROM [I04_ATH] 
	where I04_KODAS_CH LIKE @riv_id
	
	if @id is null
		SET @id = substring(@riv_id,1,3) + '000000001'

	select @sask = N01_KODAS_SS from N01_ACCT where N01_PAV_U = @g_saskaita
	if @sask is null
		SET @sask = 'NEAPRASYTA'
	begin transaction
		
		
		insert into [I04_ATH] (I04_KODAS_CH,I04_DOK_NR,I04_OP_RUSIS,I04_OP_TIPAS,I04_OP_STORNO,I04_OP_DATA,
			I04_MOKETOJAS,I04_KODAS_KS,I04_PAV,I04_ADR,I04_SUMA,I04_PERKELTA,I04_KODAS_VL,
			I04_SUMA_VAL,I04_KOEF,I04_USERIS,I04_R_DATE,I04_ADDUSR,I04_KODAS_SS,I04_APRASYMAS)
		VALUES (@id,substring(@paved_nr,1,20),1,0,0,@data,1,@m_kodas_ks,@m_pav,'',CAST(@suma as decimal(12,2)),1,@val,
		CAST(@suma as decimal(12,2)),1,'rivile-ws',cast(getdate() as smalldatetime),'rivile-ws',@sask,@pid)
		
		insert into I44_SKOL (I44_MODUL,I44_TIPAS,I44_KODAS_OP,I44_EIL_NR,I44_DOK_NR,I44_KODAS_KS,I44_KODAS_SS,
		I44_SUMA_CR,I44_KODAS_VL,I44_SUMA_CR_VL,I44_DATA_DOK,I44_DATA_MOK,I44_DATA_DSK,I44_PASTABOS,I44_ADDUSR,I44_R_DATE,I44_USERIS)
		VALUES ('CH',3,@id,1,substring(@paved_nr,1,20),@m_kodas_ks,'4441',
		CAST(@suma as decimal(12,2)),@val,CAST(@suma as decimal(12,2)),@data,@data,@data,'','rivile-ws',cast(getdate() as smalldatetime),'rivile-ws')
	
		insert into I05_ATD (I05_KODAS_CH,I05_EIL_NR,I05_DOK_NR,I05_DATA_DOK,I05_DATA_MOK,I05_DATA_DSK,I05_APR,I05_SUMA,
		I05_KODAS_SS,I05_KODAS_VL,I05_KODAS_VL_S,I05_KOEF,I05_KOEF_S,I05_SUMA_VAL,I05_SUMA_VAL_S,I05_USERIS,I05_R_DATE,I05_ADDUSR)
		VALUES (@id,1,substring(@paved_nr,1,20),@data,@data,@data,'',CAST(@suma as decimal(12,2)),
		'4441',@val,@val,1,1,CAST(@suma as decimal(12,2)),CAST(@suma as decimal(12,2)),'rivile-ws',cast(getdate() as smalldatetime),'rivile-ws')	
		
		insert into #ttresult (res) VALUES (@id)
	commit transaction
END

GO

